//
//  LBCategoryManager.m
//  jslj
//
//  Created by luckyRoy on 2017/10/10.
//  Copyright © 2017年 广东联结电子商务有限公司. All rights reserved.
//

#import "LBCategoryManager.h"
#import <LBService.h>
#import <MJExtension.h>

#define DEBUG_DEV

@implementation LBCategoryManager

+ (void)getCategoryListByBizCode:(LBCategoryBizCodeType)bizCode
                           Depth:(NSNumber *)depth
                         Success:(void(^)(NSArray <LBCategoryModel *> *modelArray))success
                            fail:(void(^)(NSString *errMsg))fail
{
    NSDictionary *params = @{
                             @"bizCode":@(bizCode),
                             @"depth":depth ? depth : @"4"
                             };
    
    [LBService get:LBURL_COMMON(URL_Category_AllModels) params:params completion:^(LBResponse *response) {
        if (response.succeed) {
            NSArray *array = [LBCategoryModel mj_objectArrayWithKeyValuesArray:response.data];
            success([self filterBankingByArray:array]);
        }
        else {
            fail(response.message);
        }
    }];
}

+ (NSArray *)filterBankingByArray:(NSArray *)array
{
 
    NSMutableArray *resultArray = [NSMutableArray array];
    
    [array enumerateObjectsUsingBlock:^(LBCategoryModel *obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if (![obj.code isEqualToString:@"5005"]) {
            [resultArray addObject:obj];
        }
    }];
    
    return resultArray;
}

+ (void)getBookingCategoryListByBizType:(NSInteger)bizType
                                Success:(void(^)(NSArray <LBCategoryModel *> *allArray,NSArray <LBCategoryModel *> *selctedArray))success
                                   fail:(void(^)(NSString *errMsg))fail
{
    NSDictionary *params = @{
                             @"bizType":@(bizType),
                             };
    
    [LBService get:LBURL_USER(URL_Category_GetBooking) params:params completion:^(LBResponse *response) {
        if (response.succeed) {
            NSArray *allArray = [LBCategoryModel mj_objectArrayWithKeyValuesArray:response.data[@"chooseSubscribe"]];
            NSArray *selectedArray = [LBCategoryModel mj_objectArrayWithKeyValuesArray:response.data[@"selectSubscribe"]];
            success([self filterBankingByArray:allArray],selectedArray);
        }
        else {
            fail(response.message);
        }
    }];
}

+ (void)saveBookingByBizCode:(NSArray *)bizCodeArray
                     BizType:(NSArray *)bizTypeArray
                     Success:(void(^)(void))success
                        fail:(void(^)(NSString *errMsg))fail;
{
    NSMutableArray *dicArray = [NSMutableArray array];
    [bizCodeArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        NSMutableDictionary *dic = [NSMutableDictionary dictionary];
        [dic setValue:obj forKey:@"bizCode"];
        [dic setValue:bizTypeArray[idx] forKey:@"bizType"];
        [dicArray addObject:dic];
    }];

    [LBService post:LBURL_USER(URL_Category_SaveBooking) params:(NSDictionary *)dicArray completion:^(LBResponse *response) {
        if (response.succeed) {
            success();
        }
        else {
            fail(response.message);
        }
    }];
}

@end
